function fmoney(s, n) //s:传入的float数字 ，n:希望返回小数点几位 
{
	n = n > 0 && n <= 20 ? n : 2;
	s = parseFloat((s + "").replace(/[^\d\.-]/g, "")).toFixed(n) + "";
	var l = s.split(".")[0].split("").reverse(),
		r = s.split(".")[1];
	t = "";
	for (i = 0; i < l.length; i++) {
		t += l[i] + ((i + 1) % 3 == 0 && (i + 1) != l.length ? "," : "");
	}
	return t.split("").reverse().join("") + "." + r;
}
window.onresize = function() {
	myresize()
};
var myresize = function() {
	console.log(1);
	$("#test-upload-drag").css("height", $("#more_img_list").width() / 3 + "px");
	$("#more_img_list>div>div").css("height", $("#more_img_list").width() / 3 + "px");
};
var onmyClick = function(obj) {
	$(obj).parent().removeClass("my-all-check");
	if (obj.checked) {
		$(obj).parent().addClass("my-all-check");
	}
}
var initget = function() {
	$(".number").focusout(function() {
		$(this).val(fmoney($(this).val(), 2));
	})
	new Sortable(document.getElementById('more_img_list'), {
		animation: 150,
		handle: '.my-handle',
		ghostClass: 'blue-background-class'
	});
	$("#test-upload-drag").css("height", $("#more_img_list").width() / 3 + "px");
	layui.use(['upload', 'form'], function() {
		var form = layui.form;
		var upload = layui.upload;
		// 库存
		form.on('checkbox(testnum)', function(data) {
			console.log(data.elem,'data.elem.checked')
			if (data.elem.checked) {
				$("#key_service2").show();
			} else {
				$("#key_service2").hide();
			}
		
		});
		form.verify({
		        //限制不为空的时候，只能输入小数
		        weight: function(value, item){
		            if (value != ""){
		                if (!/^(-)?\d+(\.\d+)?$/.test(value)){
		                    return "只能输入正整数和小数"
		                }
		            }
		        }
		    });
		form.on('checkbox(haveshopping)', function(data) {
			if (data.elem.checked) {
				$(".hasshopping").show();
				$(".noshopping").hide();
			} else {
				$(".hasshopping").hide();
				$(".noshopping").show();
			}
		});
		
		// 选项
		form.on('checkbox(specs)', function(data) {
			console.log(data.elem,'data.elem.checked')
			if (data.elem.checked) {
				$(".specs-options").show();
				$(".layui-add-card").show();
				$("#last").show();
				console.log($(this),'$(this)')
				$("data.elem").prop("checked",false);//选中某值对应的项
			} else {
				$(".specs-options").hide();
				$(".layui-add-card").hide();
				$("#last").hide();
				$("data.elem").prop("checked",true);
			}

		});
		
		
		// 厂shang
		var options = {
		        el: '#attr1',
		        name: 'xmselectName',//表单的name属性
		        layVerify: 'required',//必填项
		        tips: '产品系列',
		        toolbar: {//工具条,全选,清空,反选,自定义
		            show: false
		        },
		        data: [],
		};
		var attr1 = xmSelect.render(options)
		setTimeout(function(){
		    var data =[
		        {name: '测试数据', value:'shuiguo'},
		    ];
		    attr1.update({data: data})
		},100);
		// 厂shang
		var options = {
		        el: '#demo2',
		        name: 'xmselectName',//表单的name属性
		        layVerify: 'required',//必填项
		        tips: '产品系列',
		        toolbar: {//工具条,全选,清空,反选,自定义
		            show: true,
					list: [
					    {
					        name: '添加',
					        icon: 'layui-icon-addition',
					        method(data){
					            alert('我是添加自定义的名称');
					        }
					    } 
					] 
		        },
		        data: [],
		};
		var demo2 = xmSelect.render(options)
		setTimeout(function(){
		    var data =[
		        {name: '测试数据', value:'shuiguo'},
		    ];
		    demo2.update({data: data})
		},100);
		// 产品选项
		var options = {
		        el: '#demo1',
		        name: 'xmselectName',//表单的name属性
		        layVerify: 'required',//必填项
		        tips: '产品系列',
		        toolbar: {//工具条,全选,清空,反选,自定义
		            show: false,
		        },
		        data: [],
		};
		var demo1 = xmSelect.render(options)
		setTimeout(function(){
		    var data =[
		        {name: '主页', value:'shuiguo'},
				 {name: '测试主页', value:'shuiguo',disabled: true},
		    ];
		    demo1.update({data: data})
		},100);
		
		// 创建表单
		//动态添加input输入框
		var max = 5;
		var x = 1;
			var str = '<div class="ayui-col-md12 specs-list ">' +
				'<div class="layui-form-item">' +
					'<label class="layui-form-label labe-block">选项名称</label>' +
					 '<div class="layui-input-block input-block">'+
						'<div class="select-inner layui-form">'+
						    ' <select name="interest" lay-filter="aihao" class="select-item" lay-ignore>'+
						       '<option value="001">尺寸</option>'+
						       '<option value="002">颜色</option>'+
						        '<option value="003">材料</option>'+
						       '<option value="004">样式</option>'+
						   '</select>'+
						'</div>'+
					    '<div class="layui-button">'+
					       ' <button type="button" class="layui-btn layui-btn-normal layui-btn removeclass"><i class="layui-icon"></i></button>'+
					    '</div>'+
					'</div>'+
					'</div>' +
					'<div class="layui-form-item">' +
						'<label class="layui-form-label labe-block">选项值</label>' +
						'<div class=" layui-input-block input-block w92">' +
						   '<input type = "text" name = "username" value="黑色" lay - verify = "required" lay - reqtext = "用户名是必填项，岂能为空？" placeholder = "请输入"autocomplete = "off"class = "layui-input" >' +
						'</div>' +
						'</div>' +
						 '<div class="layui-finish" style="left: 0;">'+
						     '<button class="layui-btn" lay-submit=""  lay-filter="component-form">完成</button>'+
						 '</div>'+
					'</div>';
		$("#add").click(function() {
			if (x < max) {
			
				$("#last").append(str);
				x++;
			} else {
				layer.msg("最多添加5条信息", {
					icon: 2
				});
			}
		});
		//删除动态添加的input输入框
		    $("body").on('click', ".removeclass", function () {
		        //元素移除前校验是否被引用
		        var approvalName = $(this).parent().prev('div.specs-list').children().val();
		        var parentEle = $(this).parent().parent().parent().parent();
		        parentEle.remove();
				var number = $("#last").children().length;
				console.log(number,'number')
				if(number==0){
					$(".layui-add-card").hide();
					$("#last").append(str);
					$("#last").hide();
					$("input[name='checkbox_v1']").each(function(){
					    $(this).prop('checked', false);
					});
					layui.form.render();
					
				}
		    });
			
		
			
			
			
			
		//拖拽上传
		upload.render({
			elem: '#test-upload-drag',
			url: '/manage/business/common/upload.html?Directory=id3P7CECLN8%3D',
			multiple: true,
			done: function(res) {
				var svg =
					'<svg t="1657003356688" class="icon" viewBox="0 0 1024 1024" version="1.1" xmlns="http://www.w3.org/2000/svg" p-id="2937" width="30" height="30"><path d="M486.4 776.533333v-213.333333H247.466667v106.666667L85.333333 512l162.133334-162.133333V512h238.933333V247.466667H349.866667L512 85.333333l162.133333 162.133334h-132.266666V512h238.933333V349.866667L938.666667 512l-162.133334 162.133333v-106.666666h-238.933333v213.333333h132.266667L512 938.666667l-162.133333-162.133334h136.533333z" p-id="2938" fill="#ffffff"></path></svg>';
				$("#more_img_list").prepend("<div class='layui-col-xs4' ><div style='height:" +
					$("#more_img_list").width() / 3 +
					"px'><div class='my-all'><div class='bg-move'></div><input type='checkbox' class='my-check' onclick='onmyClick(this)' /><div class='my-handle'>" +
					svg + "</div></div><img class='' src='" + res.files[0].url +
					"' /></div></div>");
			}
		});
		 $('#addnotebook').on("click",function(){
			   $('#modal').show()
		  });
	});

}

// 主入口文件
var pjson = {};
$(function() {
	layui.config({
		version: '2015010514941530',
		base: 'http://shop120.oss-cn-shenzhen.aliyuncs.com/static/business/manage/layuiadmin/' //静态资源所在路径
	}).extend({
		index: 'lib/index' //主入口模块
	}).use(['index'], function() {
		if (typeof(initfun) != "undefined") {
			initfun();
		}
		if (top.basetop) {
			top.closeloadTip();
		}
	});
})

